package com.market.goods.web;

import com.market.goods.constance.SuperRandom;
import com.market.goods.dao.mapper.*;
import com.market.goods.dao.mongo.OrderInfo;
import com.market.goods.dao.po.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(tags = "gmall相关服务")
@RestController
@RequestMapping("/gmall")
public class GmallController {

    @Autowired
    UserInfoPOMapper userInfoPOMapper;


    @Autowired
    ActivityInfoMapper activityInfoMapper;

    @Autowired
    OrderDetailPOMapper orderDetailPOMapper;

    @Autowired
    OrderInfoPOMapper orderInfoPOMapper;

    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    SpuInfoPOMapper spuInfoPOMapper;

    @Autowired
    SkuInfoPOMapper skuInfoPOMapper;

    @Autowired
    BaseCategory3Mapper baseCategory3Mapper;

    /**
     * 增加品牌
     *
     */
    @ApiOperation(value = "增加品牌")
    @GetMapping("/addBaseTrademark")
    public String addBaseTrademark() {
        List<UserInfoPO> userInfoPOS = userInfoPOMapper.selectAll();
        for (int k = 0; k < SuperRandom.baseTrademark.length; k++) {
            BaseTrademarkPO baseTrademarkPO = new BaseTrademarkPO();
            baseTrademarkPO.setTmId(SuperRandom.idWorker.nextId()+"");
            baseTrademarkPO.setTmName(SuperRandom.baseTrademark[k]);
            baseTrademarkMapper.insertSelective(baseTrademarkPO);
        }
        return "ok";
    }

    /**
     * 添加用户信息
     *
     */
    @ApiOperation(value = "添加用户信息")
    @GetMapping("/addUserInfo")
    public void addUserInfo() {
        for (int k = 0; k < 100000; k++) {
            userInfoPOMapper.insertSelective(SuperRandom.getUserInfoPO());
        }
    }

    /**
     * 添加活动信息
     *
     */
    @ApiOperation(value = "添加活动信息")
    @GetMapping("/addActivityInfo")
    public void addActivityInfo() {
        for (int k = 0; k < 100000; k++) {
            activityInfoMapper.insertSelective(SuperRandom.getActivityInfo());
        }
    }

    /**
     * 添加spu
     *
     */
    @ApiOperation(value = "添加spu")
    @GetMapping("/addSpuInfo")
    public void addSpuInfo() {
        List<BaseCategory3PO> baseCategory3POS = baseCategory3Mapper.selectAll();
        List<BaseTrademarkPO> baseTrademarkPOS = baseTrademarkMapper.selectAll();
        for (int k = 0; k < 100000; k++) {
            BaseCategory3PO baseCategory3PO = baseCategory3POS.get(SuperRandom.getNum(0, baseCategory3POS.size() - 1));
            BaseTrademarkPO baseTrademarkPO = baseTrademarkPOS.get(SuperRandom.getNum(0, baseTrademarkPOS.size() - 1));
            spuInfoPOMapper.insertSelective(SuperRandom.getSpuInfoPO(baseCategory3PO.getId(),Long.valueOf(baseTrademarkPO.getTmId())));
        }
    }

    /**
     * 添加sku
     *
     */
    @ApiOperation(value = "添加sku")
    @GetMapping("/addSkuInfo")
    public void addSkuInfo() {
        List<SpuInfoPO> spuInfoPOS = spuInfoPOMapper.selectAll();
        for (int k = 0; k < spuInfoPOS.size(); k++) {
            SpuInfoPO spuInfoPO = spuInfoPOS.get(k);
            int num = SuperRandom.getNum(1, 29);
            for (int j = 0; j < num; j++) {
                skuInfoPOMapper.insertSelective(SuperRandom.getSkuInfoPO(spuInfoPO.getId(),spuInfoPO.getCategory3Id(),spuInfoPO.getTmId(),spuInfoPO.getSpuName()));
            }
        }
    }



    /**
     * 模拟下单
     *
     */
    @ApiOperation(value = "模拟下单")
    @GetMapping("/addOrderInfo")
    public void addOrderInfo() {
        List<UserInfoPO> userInfoPOS = userInfoPOMapper.selectAll();
        for (int k = 0; k < 100000; k++) {
            int userInfoPOSSize = userInfoPOS.size();
            UserInfoPO userInfoPO =userInfoPOS.get(SuperRandom.getNum(0,userInfoPOSSize-1));
            OrderInfoPO orderInfoPO = new OrderInfoPO();
            orderInfoPO.setId(SuperRandom.idWorker.nextId());
            orderInfoPO.setCreateTime(SuperRandom.getTime(2017));
            orderInfoPO.setExpireTime(SuperRandom.getTime(2017));
            orderInfoPO.setImgUrl("http://img.gmall.com/"+SuperRandom.idWorker.nextId()+".jpg");
            orderInfoPO.setBenefitReduceAmount(SuperRandom.getPrice(200,99));
            orderInfoPO.setFeightFee(SuperRandom.getPrice(300,99));
            orderInfoPO.setOperateTime(SuperRandom.getTime(2017));
            orderInfoPO.setOriginalTotalAmount(SuperRandom.getPrice(100000,99).add(orderInfoPO.getBenefitReduceAmount()));
            orderInfoPO.setFinalTotalAmount(orderInfoPO.getOriginalTotalAmount().subtract(orderInfoPO.getBenefitReduceAmount()).add(orderInfoPO.getFeightFee()));
            orderInfoPO.setConsigneeTel(userInfoPO.getPhoneNum());
            orderInfoPO.setConsignee(userInfoPO.getName());
            orderInfoPO.setUserId(userInfoPO.getId());
            orderInfoPO.setDeliveryAddress(SuperRandom.getAddress());
            orderInfoPO.setOrderStatus(SuperRandom.getNum(1001,1009)+"");
            orderInfoPO.setTrackingNo(SuperRandom.idWorker.nextId()+"");
            orderInfoPOMapper.insertSelective(orderInfoPO);
        }
    }

    /**
     * 添加订单明细
     *
     */
    @ApiOperation(value = "添加订单明细")
    @GetMapping("/addOrderDetail")
    public void addOrderDetail() {
        List<SkuInfoPO> skuInfoPOS = skuInfoPOMapper.selectAll();
        List<OrderInfoPO> orderInfoPOS = orderInfoPOMapper.selectAll();
        int orderInfoSize = orderInfoPOS.size();
        int size = skuInfoPOS.size();
        for (int k = 0; k < size; k++) {
            SkuInfoPO skuInfoPO = skuInfoPOS.get(k);
            int num = SuperRandom.getNum(1, 399);
            int orderNum = SuperRandom.getNum(1, orderInfoSize);
            Long orderId = orderInfoPOS.get(orderNum - 1).getId();
            for (int j = 0; j < num; j++) {
                orderDetailPOMapper.insertSelective(SuperRandom.getOrderDetailPO(skuInfoPO,orderId));
            }
        }
    }

    private int getOrderInfoSize(){
        List<OrderInfoPO> orderInfoPOS = orderInfoPOMapper.selectAll();
        return orderInfoPOS.size();
    }

}
